package com.hnkywl.sts.service.sys;

import com.hnkywl.sts.dao.sys.JsDao;
import com.hnkywl.sts.entity.sys.Js;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sys
 * 类描述:    角色服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class JsService {
    private JsDao jsDao;
    /**
     * 查询角色列表
     *
     * @param jsmc     角色名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllJs(String jsmc, int pageNo, int pageSize) {
        Page<Js> jsPage = this.jsDao.findJsList(jsmc, pageNo, pageSize);
        List<Js> jsList = jsPage.getResult() == null ? new ArrayList<Js>() : jsPage.getResult();
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("list_qxList", "mc");
        String jsJson = BaseUtils.fetchCollectionToJson(jsList, otherParam, "id", "mc", "qxList","remarks");
        return "{\"total\":" + jsPage.getTotalCount() + ",\"rows\":[" + jsJson + "]}";
    }
    /**
     * 查询角色名称列表
     *
     * @param jsmc     角色名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findJsMc(String jsmc, int pageNo, int pageSize) {
        Page<Js> jsPage = this.jsDao.findJsList(jsmc, pageNo, pageSize);
        List<Js> jsList = jsPage.getResult() == null ? new ArrayList<Js>() : jsPage.getResult();
        String jsJson = BaseUtils.fetchCollectionToJson(jsList, "id", "mc");
        return "[" + jsJson + "]";

    }

    /**
     * 查询角色明细
     *
     * @param id 角色名称
     * @return
     */
    @Transactional
    public Js findJsDetail(Long id) {
        return this.jsDao.get(id);
    }

    /**
     * 保存角色信息
     *
     * @param   js 角色实体
     * @return
     */
    @Transactional
    public void saveJs(Js js) {
        js.setDateCreated(new Date());
        js.setLastUpdated(js.getDateCreated());
        this.jsDao.save(js);

    }
    /**
     * 删除角色信息
     *
     * @param id 角色ID
     * @return
     */
    @Transactional
    public void deleteJs(Long id) {
        Js js = jsDao.get(id);
        if (!js.getGwList().isEmpty() || !js.getYgList().isEmpty()) {
            throw new RuntimeException("此角色已被使用，不能删除！");
        }
        js.setLastUpdated(new Date());
        js.setDeleted(1);
        this.jsDao.save(js);
    }

    /**
     * ajax远程验证
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id,String value, String field) {
        return jsDao.checkUnique(id,value,field);
    }
    @Autowired
    public void setJsDao(JsDao jsDao) {
        this.jsDao = jsDao;
    }
}
